package ga.stopcriterias;

import ga.individuals.Individual;

import java.util.Collection;
import java.util.Collections;

public class ContentStrategy implements StopStrategy {
	private Double prevBestFitness;
	private double tol;
	private double i;
	
	public ContentStrategy(double tol) {
		this.tol = tol;
	}
	
	@Override
	public boolean execute(Collection<Individual> population) {
		Individual best = Collections.max(population);
		
		if (prevBestFitness == null) {
			prevBestFitness = best.getFitnessValue();
			i++;
			return false;
		}  else if ((i%10 == 0) && Math.abs((prevBestFitness - best.getFitnessValue())) <= tol) {
			return true;
		} else {
			if (i % 10 == 0) {
				prevBestFitness = best.getFitnessValue();
			}
			i++;
			return false;
		}
	}

}
